package com.example.test.config;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.List;

public class  MySelector implements ImportSelector {

    private Class<?>getSpringFactoriesLoaderFactoryClass(){
        //spring.factories文件里面的key,可以是任何一个普通类
        return MyEnableAutoConfig.class;
    }

    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        List<String> configurations = getConditionConfigurations();
        return StringUtils.toStringArray(configurations);
        //return new String[]{"com.example.test.config.ConfigC"};
    }

    protected List<String> getConditionConfigurations(){
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(
                getSpringFactoriesLoaderFactoryClass(),MySelector.class.getClassLoader());
        Assert.notEmpty(configurations,"no config");
        System.out.println("*****************configurations.toArray()*******************"+MyEnableAutoConfig.class.getName());
        System.out.println("*****************configurations.toArray()*******************"+configurations.toString());
        return configurations;
    }

}